import request from '@/utils/request'

export function searchTeam(code) {
  return request({
    url: '/teams',
    method: 'get',
    params: { teamCode: code }
  })
}

export function fetchTeamInfo(teamId) {
  return request({
    url: '/teams/' + teamId,
    method: 'get'
  })
}

export function updateTeamInfo(teamId, { name, classes, allow_join, years }) {
  return request({
    url: '/teams/' + teamId,
    method: 'patch',
    data: {
      name, classes, allow_join, years
    }
  })
}

export function join({ teamId, userId, cadre, _class, dormitory, room, qq, wechat }) {
  return request({
    url: '/teams/' + teamId + '/users/' + userId,
    method: 'post',
    data: { cadre, class: _class, dormitory, room, qq, wechat }
  })
}

export function create({ name, years, classes, dormitories }) {
  return request({
    url: '/teams',
    method: 'post',
    data: { name, years, classes, dormitories }
  })
}

export function fetchTags(teamId) {
  return request({
    url: '/teams/' + teamId + '/managementtags',
    method: 'get'
  })
}

export function addTag(teamId, name) {
  return request({
    url: '/teams/' + teamId + '/managementtags',
    method: 'post',
    data: { name }
  })
}

export function updateTag(teamId, newTag) {
  return request({
    url: '/teams/' + teamId + '/managementtags/' + newTag._id,
    method: 'put',
    data: { newTag }
  })
}

export function deleteTag(teamId, tagId) {
  return request({
    url: '/teams/' + teamId + '/managementtags/' + tagId,
    method: 'delete'
  })
}

export function fetchMembers(teamId, page, per_page) {
  return request({
    url: '/teams/' + teamId + '/users',
    params: { page, per_page },
    method: 'get'
  })
}

export function addTagToSbInTeam(tagId, userId, teamId) {
  return request({
    url: '/teams/' + teamId + '/users/' + userId + '/managementtags/' + tagId,
    method: 'post'
  })
}

export function removeTagFromSbInTeam(tagId, userId, teamId) {
  return request({
    url: '/teams/' + teamId + '/users/' + userId + '/managementtags/' + tagId,
    method: 'delete'
  })
}
